from fastapi import APIRouter, Request
from fastapi import Depends
from requests import Session

from config.get_db import get_db
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsGaodeFenceTerminalVO
from core.service.cms_gaode_fence_terminal_service import CmsGaodeFenceTerminalService
from utils.log_util import *
from utils.response_util import *

gaodeFenceController = APIRouter()


@gaodeFenceController.post("/gaode_fence_termianl/query_by_id", response_model=CmsGaodeFenceTerminalVO)
async def get_gaode_fence_by_id(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_fence_terminal = CmsGaodeFenceTerminalService.get_cms_gaode_fence_terminal_by_id(query_db, id)
        logger.info('获取成功')
        return response_200(data=cms_gaode_fence_terminal, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeFenceController.post("/gaode_fence_termianl/query_by_fence_id", response_model=CmsGaodeFenceTerminalVO)
async def get_gaode_fence_by_fence_id(request: Request, fence_id: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_fence_terminal = CmsGaodeFenceTerminalService.get_cms_gaode_fence_terminal_by_fence_id(query_db,
                                                                                                         fence_id)
        logger.info('获取成功')
        return response_200(data=cms_gaode_fence_terminal, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeFenceController.post("/gaode_fence_termianl/query_by_terminal_id", response_model=CmsGaodeFenceTerminalVO)
async def get_gaode_fence_like_fence_name(request: Request, terminal_id: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_fence_terminal_list = CmsGaodeFenceTerminalService.get_cms_gaode_fence_terminal_by_terminal_id(
            query_db, terminal_id)
        logger.info('获取成功')
        return response_200(data=cms_gaode_fence_terminal_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeFenceController.post("/gaode_fence_termianl/bind", response_model=CrudResponse)
async def add_gaode_fence(request: Request, cms_gaode_fence_terminal: CmsGaodeFenceTerminalVO,
                          query_db: Session = Depends(get_db)):
    try:
        cms_gaode_fence_terminal_result = await CmsGaodeFenceTerminalService.bind_cms_gaode_fence_terminal(request,
                                                                                                           query_db,
                                                                                                           cms_gaode_fence_terminal)
        if cms_gaode_fence_terminal_result.is_success:
            logger.info(cms_gaode_fence_terminal_result.message)
            return response_200(data=cms_gaode_fence_terminal_result, message=cms_gaode_fence_terminal_result.message)
        else:
            logger.warning(cms_gaode_fence_terminal_result.message)
            return response_400(data="", message=cms_gaode_fence_terminal_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeFenceController.patch("/gaode_fence_termianl/unbind", response_model=CrudResponse)
async def edit_gaode_fence(request: Request, cms_gaode_fence_terminal: CmsGaodeFenceTerminalVO,
                           query_db: Session = Depends(get_db)):
    try:
        cms_gaode_fence_terminal_result = await CmsGaodeFenceTerminalService.unbind_cms_gaode_fence_terminal(query_db,
                                                                                                             cms_gaode_fence_terminal)
        if cms_gaode_fence_terminal_result.is_success:
            logger.info(cms_gaode_fence_terminal_result.message)
            return response_200(data=cms_gaode_fence_terminal_result, message=cms_gaode_fence_terminal_result.message)
        else:
            logger.warning(cms_gaode_fence_terminal_result.message)
            return response_400(data="", message=cms_gaode_fence_terminal_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
